package com.ckd.flyingtrip.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.MainActivity;
import com.ckd.flyingtrip.activity.MeBatteryActivity;
import com.ckd.flyingtrip.activity.MeCarActivity;
import com.ckd.flyingtrip.activity.MyVehicleActivity;
import com.ckd.flyingtrip.activity.OptionActivity;
import com.ckd.flyingtrip.activity.UserInfoActivity;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.UserInfoBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.CustomDialog;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MeFragment extends Fragment {

    @BindView(R.id.fragment_me_icon)
    CircleImageView icon;
    @BindView(R.id.fragment_me_phone)
    TextView phone;
    @BindView(R.id.fragment_me_legalize)
    LinearLayout legalize;
    @BindView(R.id.fragment_me_legalizeName)
    TextView legalizeName;
    @BindView(R.id.fragment_me_rent)
    TextView rent;
    @BindView(R.id.fragment_me_batteryRent)
    TextView batteryRent;
    @BindView(R.id.fragment_me_carTime)
    LinearLayout carTime;
    @BindView(R.id.fragment_me_carTimeText)
    TextView carTimeText;
    @BindView(R.id.fragment_me_batteryTime)
    LinearLayout batteryTime;
    @BindView(R.id.fragment_me_batteryTimeText)
    TextView batteryTimeText;

    private CustomDialog.Builder builder;
    private CustomDialog dialog;
    private String dataPhone = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, null);
        ButterKnife.bind(this, view);

        initView();
        initData();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (BcUtils.getUserInfo().getUserHeadUrl() != null && !BcUtils.getUserInfo().getUserHeadUrl().equals(""))
            Glide.with(getActivity()).load(BcUtils.getUserInfo().getUserHeadUrl()).into(icon);
        if (BcUtils.getUserInfo().getUserDepositNum() != null && !BcUtils.getUserInfo().getUserDepositNum().equals(""))
            batteryRent.setText("¥" + BcUtils.getUserInfo().getUserDepositNum());
        if (BcUtils.getUserInfo().getUserCarDepositNum() != null && !BcUtils.getUserInfo().getUserCarDepositNum().equals(""))
            rent.setText("¥" + BcUtils.getUserInfo().getUserCarDepositNum());

        if (BcUtils.getUserInfo().getUserBindingNumEd().equals("0") || BcUtils.getUserInfo().getUserBindingNumEd().equals("")) {
            batteryTime.setVisibility(View.GONE);
        } else {
            batteryTime.setVisibility(View.VISIBLE);
            batteryTimeText.setText(BcUtils.getUserInfo().getUserDueTime());
        }

        if (BcUtils.getUserInfo().getBindingElectricBicycle().equals("")) {
            carTime.setVisibility(View.GONE);
        } else {
            carTime.setVisibility(View.VISIBLE);
            carTimeText.setText(BcUtils.getUserInfo().getUserCarDueTime());
        }

        if (BcUtils.getUserInfo().getUserState().equals("2")) {
            legalize.setVisibility(View.GONE);
        } else {
            legalize.setVisibility(View.VISIBLE);
            legalizeName.setText(BcUtils.getUserInfo().getUserName());
        }

    }

    private void initView() {
        phone.setText(BcUtils.getUserInfo().getUserPhone());
        initDialog();
    }

    private void initDialog() {
        builder = new CustomDialog.Builder(getActivity())
                .cancelTouchout(false)
                .view(R.layout.dialog_me_client)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y360)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_me_client_btn1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                })
                .addViewOnclick(R.id.dialog_me_client_btn2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        Tools.callPhone(getActivity(), dataPhone);
                    }
                });

        dialog = builder.build();
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.KEFU, new Response.Listener<String>() {

            @Override
            public void onResponse(String s) {
                Log.e("客服电话", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    if (code.equals("1")) {
                        dataPhone = json.getString("data");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("token", BcUtils.getToken());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    @OnClick({R.id.fragment_me_setup, R.id.fragment_me_car, R.id.fragment_me_info, R.id.fragment_me_client, R.id.fragment_me_battery})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.fragment_me_setup:
                intent = new Intent(getActivity(), OptionActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_me_car:
//                intent = new Intent(getActivity(), MyVehicleActivity.class);
                intent = new Intent(getActivity(), MeCarActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_me_info:
                intent = new Intent(getActivity(), UserInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_me_client:
                dialog.show();
                break;
            case R.id.fragment_me_battery:
                intent = new Intent(getActivity(), MeBatteryActivity.class);
                startActivity(intent);
                break;
        }
    }
}
